package com.zlq.cn.controller.sign.movie;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlq.cn.api.SignApiResult;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.dto.param.MovieCommentPageParam;
import com.zlq.cn.dto.param.MovieReportParam;
import com.zlq.cn.entity.MovieComment;
import com.zlq.cn.entity.MovieDisable;
import com.zlq.cn.entity.SuggestRecord;
import com.zlq.cn.entity.User;
import com.zlq.cn.service.movie.MovieCommentService;
import com.zlq.cn.service.un.SuggestRecordService;
import com.zlq.cn.service.movie.MovieDisableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Optional;

/**
 * <p>
 * 影片评论 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2022-03-19
 */
@Api(tags = "影片-评论")
@RestController
@RequestMapping("/movie/comment")
public class MovieCommentController {

    @Resource
    private MovieCommentService movieCommentService;

    @Resource
    private SuggestRecordService suggestRecordService;

    @Resource
    private MovieDisableService movieDisableService;

    @ApiOperation("分页获取影片评论列表")
    @PostMapping("/page")
    public SignApiResult<Page<MovieComment>> page(@Valid @RequestBody MovieCommentPageParam pageParam) {
        pageParam.setUserId(Optional.ofNullable(SignApiBootstrap.loginUser()).map(User::getId).orElse(null));
        return SignApiResult.resultSuccess(movieCommentService.page(pageParam));
    }

    @ApiOperation("发布评论")
    @PostMapping("/add")
    public SignApiResult<Boolean> add(@Valid @RequestBody MovieComment movieComment) {
        User user = SignApiBootstrap.requestUser();
        movieComment.setAvatarHead(user.getAvatar())
                .setAvatarName(user.getName())
                .setStatus(1)
                .setTime(LocalDateTime.now())
                .setUserId(user.getId());
        return SignApiResult.resultSuccess(movieCommentService.save(movieComment));
    }

    @ApiOperation("屏蔽评论")
    @PostMapping("/disable")
    public SignApiResult<String> disable(@RequestBody MovieComment movieComment) {
        User user = SignApiBootstrap.requestUser();
//        if (Objects.isNull(user.getVipLevel()) || Config.LEVEL_ADMIN != user.getVipLevel()) {
//            return SignApiResult.resultError("无权限");
//        }
        Assert.notNull(movieComment.getMovieCommentId(), "评论id不能为空");
        movieDisableService.save(new MovieDisable()
                .setUserId(user.getId())
                .setDataId(movieComment.getMovieCommentId())
                .setType("MOVIE_COMMENT")
                .setDisable(1));
        return SignApiResult.resultSuccess("屏蔽成功");
    }

    @ApiOperation("举报")
    @PostMapping("/report")
    public SignApiResult<String> report(@RequestBody MovieReportParam param) {
        SuggestRecord suggestRecord = new SuggestRecord()
                .setUserid(SignApiBootstrap.requestUser().getId())
                .setContent(param.getContent())
                .setMobile(String.valueOf(param.getMovieCommentId()))
                .setSubmitdate(LocalDateTime.now());
        suggestRecordService.save(suggestRecord);
        return SignApiResult.resultSuccess("举报成功");
    }

}

